<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户手机号" prop="userTel">
        <el-input
            v-model="queryParams.userTel"
            placeholder="请输入用户手机号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCard">
        <el-input
            v-model="queryParams.orderCard"
            placeholder="请输入订单号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="航班号" prop="flightNumber">
        <el-input
            v-model="queryParams.flightNumber"
            placeholder="请输入航班号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="航站楼信息" prop="terminalInformation">
        <el-input
            v-model="queryParams.terminalInformation"
            placeholder="请输入航站楼信息"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机场名称" prop="startPoint">
        <el-input
            v-model="queryParams.startPoint"
            placeholder="请输入机场名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-input
            v-model="queryParams.destination"
            placeholder="请输入目的地"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接单司机id" prop="driverId">
        <el-input
            v-model="queryParams.driverId"
            placeholder="请输入接单司机id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['pickAirport:pickAirport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['pickAirport:pickAirport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['pickAirport:pickAirport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['pickAirport:pickAirport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="pickAirportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="接机订单id" align="center" prop="pickairportId" />
              <el-table-column label="用户id" align="center" prop="userId" />
              <el-table-column label="用户手机号" align="center" prop="userTel" />
              <el-table-column label="订单号" align="center" prop="orderCard" />
              <el-table-column label="航班号" align="center" prop="flightNumber" />
              <el-table-column label="航站楼信息" align="center" prop="terminalInformation" />
              <el-table-column label="机场名称" align="center" prop="startPoint" />
              <el-table-column label="目的地" align="center" prop="destination" />
              <el-table-column label="距离" align="center" prop="distance" />
              <el-table-column label="上车时间" align="center" prop="gocarTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.gocarTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="接单司机id" align="center" prop="driverId" />
              <el-table-column label="接单时间" align="center" prop="startTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="结束时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="预估价格" align="center" prop="predictPrice" />
              <el-table-column label="实际价格" align="center" prop="realPrice" />
              <el-table-column label="订单状态" align="center" prop="orderStatus" >
                <template #default="{row}">
                <span v-if="row.orderStatus == 1">待接单</span>
                <span v-if="row.orderStatus == 2">已接单/待上车</span>
                <span v-if="row.orderStatus == 3">已上车</span>
                <span v-if="row.orderStatus == 4">待支付</span>
                <span v-if="row.orderStatus == 5">已支付/已结束/待评价</span>
                <span v-if="row.orderStatus == 6">已评价</span>
                </template>
              </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.orderStatus==4" link type="primary" icon="Delete" @click="payPickAirport(scope.row)" >支付</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改接机订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      {{form}}
      <el-form ref="pickAirportRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="航班号" prop="flightNumber">
          <el-input v-model="form.flightNumber" placeholder="请输入航班号" />
        </el-form-item>
        <el-form-item label="航站楼信息" prop="terminalInformation">
          <el-input v-model="form.terminalInformation" placeholder="请输入航站楼信息" />
        </el-form-item>
        <el-form-item label="机场名称" prop="startPoint">
          <el-input v-model="form.startPoint" placeholder="请输入机场名称" @blur="findStartAddrs(form.startPoint)" />
        </el-form-item>
        <el-form-item label="目的地" prop="destination">
          <el-input v-model="form.destination" placeholder="请输入目的地" @blur="findEndAddrs(form.destination)" />
        </el-form-item>
        <el-form-item label="距离" prop="distance">
          <el-input v-model="form.distance" placeholder="请输入距离" />
        </el-form-item>
        <el-form-item label="上车时间" prop="gocarTime">
          <el-date-picker clearable
                          v-model="form.gocarTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择上车时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预估价格" prop="predictPrice">
          <el-input v-model="form.predictPrice" placeholder="请输入预估价格" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PickAirport">
  import { listPickAirport, getPickAirport, delPickAirport, addPickAirport, updatePickAirport } from "@/api/pickAirport/pickAirport";
  import {alipayTradePagePay, findAddr} from "@/api/passenger/ChatMsg.js";

  const { proxy } = getCurrentInstance();

  const pickAirportList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      userId: null,
      userTel: null,
      orderCard: null,
      flightNumber: null,
      terminalInformation: null,
      startPoint: null,
      startPointLongitude: null,
      startPointLatitude: null,
      destination: null,
      destinationLongitude: null,
      destinationLatitude: null,
      distance: null,
      gocarTime: null,
      driverId: null,
      startTime: null,
      endTime: null,
      predictPrice: null,
      realPrice: null,
      orderStatus: null
    },
    rules: {
    }
  });

  const { queryParams, form, rules } = toRefs(data);

  /** 查询接机订单列表 */
  function getList() {
    loading.value = true;
    listPickAirport(queryParams.value).then(response => {
            pickAirportList.value = response.data.rows;
      total.value = response.data.total;
      loading.value = false;
    });
  }

function payPickAirport(row) {
  alipayTradePagePay({orderCard:row.orderCard+"_3",payType:"zfb"}).then(
      res=>{
        document.write(res.data)
      }
  )
}
function  findStartAddrs(addr){
  findAddr(addr).then(obj=>{
    if (obj.code===200){
      let location = obj.data.geocodes[0].location;
      let split = location.split(",");
      form.value.startPointLongitude = split[0]
      form.value.startPointLatitude = split[1]
    }
  })
}

function findEndAddrs(addr){
  findAddr(addr).then(obj=>{
    if (obj.code===200){
      let location = obj.data.geocodes[0].location;
      let split = location.split(",");
      form.value.destinationLongitude = split[0]
      form.value.destinationLatitude = split[1]
    }
  })
}

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      pickairportId: null,
      userId: null,
      userTel: null,
      orderCard: null,
      flightNumber: null,
      terminalInformation: null,
      startPoint: null,
      startPointLongitude: null,
      startPointLatitude: null,
      destination: null,
      destinationLongitude: null,
      destinationLatitude: null,
      distance: null,
      createTime: null,
      gocarTime: null,
      driverId: null,
      startTime: null,
      endTime: null,
      predictPrice: null,
      realPrice: null,
      orderStatus: null
    };
    proxy.resetForm("pickAirportRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.pickairportId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加接机订单";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _pickairportId = row.pickairportId || ids.value
    getPickAirport(_pickairportId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改接机订单";
    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["pickAirportRef"].validate(valid => {
      if (valid) {
        if (form.value.pickairportId != null) {
          updatePickAirport(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addPickAirport(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _pickairportIds = row.pickairportId || ids.value;
    proxy.$modal.confirm('是否确认删除接机订单编号为"' + _pickairportIds + '"的数据项？').then(function() {
      return delPickAirport(_pickairportIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('pickAirport/pickAirport/export', {
      ...queryParams.value
    }, `pickAirport_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
